package com.ysh.xiaomi.web.filter;


import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 袁胜虎 on 2022/8/18 11:49
 */
@WebFilter(urlPatterns = {"/cart"})
public class AuthenFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //我们登录后会保存一个数据到session,所以要判断有没有登录,就看session中有没有这个数据,没有就说明没有经过登录
        Object user = ((HttpServletRequest)servletRequest).getSession().getAttribute("user");
        if (user == null){
            ((HttpServletResponse)servletResponse).sendRedirect("/login.jsp");
            return;
        }
        //交给我们的过滤器链执行下一个操作,责任链设计模式
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}
